package com.guan.poi.xml.util;

import com.guan.poi.tmp.Tmp;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * 字符串处理
 */
public class XStringUtil {

    /**
     * 获取数值结束的位置及数值 起始位必须是数字
     */
    public static Tmp<Integer, Number> getNum(String eval) {
        boolean hasSpot=false;
        char[] chars = eval.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if('.'==aChar) {
                hasSpot=true;
                continue;
            }
            if(Character.isDigit(aChar)) {
                continue;
            }

            if('l'==aChar || 'L'==aChar) {
                return new Tmp<>(i, Long.valueOf(eval.substring(0, i)));
            }
            if('f'==aChar || 'F'==aChar) {
                return new Tmp<>(i, Float.valueOf(eval.substring(0, i)));
            }
            if('d'==aChar || 'D'==aChar) {
                return new Tmp<>(i, Double.valueOf(eval.substring(0, i)));
            }
            return new Tmp<>(i-1, Integer.valueOf(eval.substring(0, i)));
        }
        if(hasSpot) {
            return new Tmp<>(eval.length()-1, Double.valueOf(eval));
        }
        return new Tmp<>(eval.length()-1, Integer.valueOf(eval));
    }

    /**
     * 数组内任意字符在字符串内的起始位置
     * @param start start index
     */
    public static int indexOfAny(String str, char[] searchChars, int start) {
        if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(searchChars)) {
            return StringUtils.INDEX_NOT_FOUND;
        }
        int csLen = str.length();
        for (int i = start; i <= csLen-1; i++) {
            char ch = str.charAt(i);
            for (char searchChar : searchChars) {
                if (searchChar == ch) {
                    return i;
                }
            }
        }
        return StringUtils.INDEX_NOT_FOUND;
    }

    /**
     * 数组内任意字符在字符串内的最后位置
     */
    public static int lastIndexOfAny(String str, char[] searchChars) {
        if (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(searchChars)) {
            return StringUtils.INDEX_NOT_FOUND;
        }
        int csLen = str.length();
        for (int i = csLen-1; i >=0 ; i--) {
            char ch = str.charAt(i);
            for (char searchChar : searchChars) {
                if (searchChar == ch) {
                    return i;
                }
            }
        }
        return StringUtils.INDEX_NOT_FOUND;
    }


    /**
     * 获取括号开始位置
     */
    public static int getParenthesesStart(String eval, int end) {
        return getWrapStart(eval, end, '(', ')');
    }

    /**
     * 获取括号结束位置
     */
    public static int getParenthesesEnd(String eval, int start) {
        return getWrapEnd(eval, start, '(', ')');
    }

    public static int getArrayEnd(String eval, int start) {
        return getWrapEnd(eval, start, '[', ']');
    }

    /**
     * 获取括号开始位置
     * @param eval 字符串
     * @param endIndex right所在位置
     * @return 对应left的位置
     */
    public static int getWrapStart(String eval, int endIndex, char left, char right) {
        int num = 0;
        for (int i = endIndex - 1; i >= 0; i--) {
            if (i > 0 && eval.charAt(i - 1) == '\\') {
                int cNum = 0, cIndex = i - 1;
                while (cIndex > 0 && eval.charAt(--cIndex) == '\\') {
                    cNum++;
                }
                if (cNum % 2 == 0) {
                    i--;
                    continue;
                }
            }
            char ch = eval.charAt(i);
            if (ch == right) {
                num++;
            } else if (ch == left) {
                if (--num < 0) {
                    return i;
                }
            }
        }
        return -1;
    }


    /**
     * 获取括号结束位置
     * @param eval 字符串
     * @param beginIndex left所在位置
     * @return 对应的right位置
     */
    public static int getWrapEnd(String eval, int beginIndex, char left, char right) {
        int num = 0;
        for (int i = beginIndex + 1; i < eval.length(); i++) {
            char ch = eval.charAt(i);
            if (ch == '\\') {
                i++;
            } else if (ch == left) {
                num++;
            } else if (ch == right) {
                if (--num < 0) {
                    return i;
                }
            }
        }
        return -1;
    }
    
}
